<?php
/**
 * APP版本服务
 * User: xwc
 * Date: 2024/01/15
 */

namespace App\Service\Core\Sys\Config;

use App\Model\Core\Sys\AppVersionModel;
use App\Service\Core\CoreBaseService;
use App\Traits\SingleInstanceTrait;

class AppVersionService extends CoreBaseService
{
    use SingleInstanceTrait;

    private AppVersionModel $AppVersionModel;

    protected function __construct()
    {
        $this->AppVersionModel = AppVersionModel::getInstance();
    }

    /**
     * 获取最新的一个
     * @param string $app_platform
     * @return array
     * @throws \Exception
     */
    public function getLastOne(string $app_platform): array
    {
        $version_info = $this->AppVersionModel->getLastOne($app_platform);
        if (!empty($version_info)) {
            $version_info['app_file_url'] = easy_cloud_attached($version_info['app_file']);
        }
        return $version_info;
    }

    /**
     * 删除一个
     * @param int $id
     * @param $operate_id
     * @param $operate_by
     * @return array
     */
    public function delOne(int $id, $operate_id, $operate_by): array
    {
        if ($id <= 0) {
            return alert_info(1, '参数错误');
        }
        $old_info = $this->getOne($id);
        if (empty($old_info)) {
            return alert_info(1, '记录不存在');
        }
        $res = $this->AppVersionModel->deleteOne($id);
        if (!$res) {
            return alert_info(1, '删除失败');
        }
        $this->addLog('APP版本号', '删除APP版本号' . ' id:' . $id, $operate_id, $operate_by);
        return alert_info(0, '分配成功');
    }

    /**
     * 获取单个
     * @param int $id
     * @return array
     */
    public function getOne(int $id): array
    {
        return $this->AppVersionModel->getOne($id);
    }
}
